package com.tokenplus.thai.me

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.tokenplus.thai.aviewmodel.CheckinViewModel
import com.tokenplus.thai.base.BaseActivity
import com.tokenplus.thai.base.BaseFragment
import com.tokenplus.thai.components.dialog.CheckinRemindDialog
import com.tokenplus.thai.data.ReqBase
import com.tokenplus.thai.databinding.FragmentMeBinding
import com.tokenplus.thai.extend.*
import com.tokenplus.thai.home.HomeActivity


class MeHomeFragment : BaseFragment() {

    private lateinit var mFragmentBinding: FragmentMeBinding

    // checkin viewModel
    private val mCheckinViewModel by lazy {
        ViewModelProvider(this)[CheckinViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mCheckinViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mFragmentBinding =
            FragmentMeBinding.inflate(inflater, container, false)
        return mFragmentBinding.root
    }

    override fun onResume() {
        super.onResume()

        //获取通知信息
        getNotifyRedMarkInfo()
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {
            // 监听接品返回
            onHandleLiveDataCallback()

            //点击 manager my wallet
            mFragmentBinding.rlMyManagerMyWallet.setOnClickListener {
                (requireActivity() as BaseActivity).startToMyWalletActivity()
            }

            //点击 check in
            mFragmentBinding.rlMyCheckin.setOnClickListener {
                (requireActivity() as BaseActivity).startToCheckinActivity()
            }

            //点击 coupons
            mFragmentBinding.rlMyCoupons.setOnClickListener {
                (requireActivity() as BaseActivity).startToCouponsActivity()
            }

            //点击 my earn
            mFragmentBinding.rlMyMyEarn.setOnClickListener {
                (requireActivity() as HomeActivity).toEarnTab()
            }

            //点击 invite
            mFragmentBinding.rlMyInvite.setOnClickListener {
                (requireActivity() as BaseActivity).startToInviteActivity()
            }

            //点击 setting
            mFragmentBinding.rlMySetting.setOnClickListener {
                (requireActivity() as BaseActivity).startToSettingActivity()
            }

            //点击 term of use
            mFragmentBinding.rlMyTermsOfUse.setOnClickListener {
                (requireActivity() as BaseActivity).startToTermOfUseActivity()
            }

            //点击 about us
            mFragmentBinding.rlMyAboutUs.setOnClickListener {
                (requireActivity() as BaseActivity).startToAboutUsWebViewActivity()
            }

            //点击 contact us
            mFragmentBinding.rlMyCustomerService.setOnClickListener {
                (requireActivity() as BaseActivity).startToCustomerServiceActivity()
            }

            //点击 退出
            mFragmentBinding.submitMyLogout.setOnClickListener {
                (requireActivity() as BaseActivity).startToLoginActivity()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    private fun onHandleLiveDataCallback() {
        mCheckinViewModel.onGetRedMarkInfoLiveData.observe(viewLifecycleOwner) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    val showCouponsRedMark = (result.coupons ?: "") == "YES"
                    if (showCouponsRedMark) {
                        mFragmentBinding.vMyCouponsRedMark.visibility = View.VISIBLE
                    } else {
                        mFragmentBinding.vMyCouponsRedMark.visibility = View.GONE
                    }

                    val showCheckinRedMark = (result.checkIn ?: "") == "YES"
                    if (showCheckinRedMark) {
                        mFragmentBinding.vMyCheckinRedMark.visibility = View.VISIBLE
                    } else {
                        mFragmentBinding.vMyCheckinRedMark.visibility = View.GONE
                    }

                    val showTabRedMark = (showCheckinRedMark or showCouponsRedMark)
                    (requireActivity() as HomeActivity).makeEarnTabRedMark(showTabRedMark)
                }
            }
        }
    }

    /** 获取红点通知信息 */
    private fun getNotifyRedMarkInfo() {
        val reqBase = ReqBase()
        reqBase.showLoading = false
        mCheckinViewModel.getRedMarkInfo(reqBase)
    }
}